package org.dreamwork.jasmine2.editor;

import org.dreamwork.config.ConfigParseException;
import org.dreamwork.config.IXMLConfigParser;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by IntelliJ IDEA.
 * User: seth.yang
 * Date: 12-7-9
 * Time: 下午4:00
 */
public class MappingConfigParser implements IXMLConfigParser {
    public Map<String, String> parse (Element section) throws ConfigParseException {
        Map<String, String> mappings = new HashMap<String, String> ();
        NodeList list = section.getElementsByTagName ("mapping");
        for (int i = 0; i < list.getLength (); i ++) {
            Element e = (Element) list.item (i);
            if (!e.hasAttribute ("name"))
                throw new ConfigParseException ("Attribute of element <mapping> must be specified!");
            String key = e.getAttribute ("name");
            String url = e.getTextContent ();
            mappings.put (key, url.replace ('.', '/'));
        }
        return mappings;
    }
}